package gin_http

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http/pprof"
)

// PProfOption pprof支持选项
type PProfOption struct {
	Enable bool  `json:"Enable" yaml:"Enable"`
	Port   int64 `json:"Port" yaml:"Port"` //用于pprof的端口号，如： 8808
}

// PProfRouter 注册路由,注册必须为gin.Engine，防止有前缀
func (s PProfOption) PProfRouter(r *gin.Engine) {
	if !s.Enable {
		return
	}
	r.Any("/debug/pprof/", gin.WrapF(pprof.Index))
	r.Any("/debug/pprof/:name", gin.WrapF(pprof.Index))
	r.Any("/debug/pprof/cmdline", gin.WrapF(pprof.Cmdline))
	r.Any("/debug/pprof/profile", gin.WrapF(pprof.Profile))
	r.Any("/debug/pprof/symbol", gin.WrapF(pprof.Symbol))
	r.Any("/debug/pprof/trace", gin.WrapF(pprof.Trace))
}

// Start 开始
func (s PProfOption) Start() error {
	if !s.Enable {
		return nil
	}
	engine := gin.New()

	s.PProfRouter(engine)

	return engine.Run(fmt.Sprintf(":%d", s.Port))
}
